package com.kdtech.analyse.NewsPaper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

public class FznewsNewsPaperAnalyse implements AnalyseNews {

	
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		//http://mag.fznews.com.cn/html/fzrb/20121120/fzrb373892.html
		//http://mag.fznews.com.cn/html/fzwb/20121120/fzwb373745.html
		
		String[] regex = {
				"http://mag.fznews.com.cn/html/(fzrb|fzwb)/[0-9]{8}/(fzrb|fzwb)[0-9]{3,10}.html",
				"http://mag.fznews.com.cn/shtml/(fzrb|fzwb)/[0-9]{8}/[0-9].*.shtml"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {		
		NewsMeta newspaper = new NewsMeta();
				
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();	
		newspaper.setUrl(url);
		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		Document doc = Jsoup.parse(html);
		
		title =doc.select("html body table tbody tr td table#Table14 tbody tr td table#Table15 tbody tr td table#Table17 tbody tr td[height=40]").text();
		if(title==null || title.trim().length()==0){
			title =doc.select("h1").text();
		}
		
		date=DateUtils.matchDate(url.replace("/", "-"));
		content = HtmlCleaner.getContentHtml(url,doc.select("div#content_div"));
		
		newspaper.setTitle(StringUtils.trimSpace(title));
		newspaper.setContent(StringUtils.trimSpace(content));
		newspaper.setDate(date);
		return newspaper;
	}
	
	
	public boolean isTaskPage(String url) {
		boolean bRet = false;
		//http://mag.fznews.com.cn/html/fzrb/20121120/fzrb2.html
		String[] regex = {
				"http://mag.fznews.com.cn/html/(fzrb|fzwb)/[0-9]{8}/(fzrb|fzwb)[0-9]{0,2}.html",
				"http://mag.fznews.com.cn/shtml/(fzrb|fzwb)/[0-9]{8}/.*.shtml"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}
	
	
}
